const stringFromDate = (date) => {
  if (typeof date === "string") {
    date = new Date(date);
  }
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, "0");
  const day = date.getDate().toString().padStart(2, "0");
  const formattedDate = `${year}-${month}-${day}`;
  return formattedDate;
};

const stringFromDateTime = (date) => {
  if (typeof date === "string") {
    date = new Date(date);
  }
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, "0");
  const day = date.getDate().toString().padStart(2, "0");
  const hour = date.getHours().toString().padStart(2, "0");
  const minute = date.getMinutes().toString().padStart(2, "0");
  const second = date.getSeconds().toString().padStart(2, "0");
  const formattedDate = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  return formattedDate;
};

const getCurrentTimestamp = () => {
  // 获取当前时间戳（毫秒）
  const timestampInMilliseconds = Date.now();

  // 将毫秒转换为秒
  const timestampInSeconds = Math.floor(timestampInMilliseconds / 1000);

  return timestampInSeconds;
};

const getCurrentTimestamp2 = () => {
  const now = new Date();
  const year = now.getFullYear(); // 获取四位年份
  const month = String(now.getMonth() + 1).padStart(2, "0"); // 获取月份，并补零
  const day = String(now.getDate()).padStart(2, "0"); // 获取日期，并补零
  const hours = String(now.getHours()).padStart(2, "0"); // 获取小时，并补零
  const minutes = String(now.getMinutes()).padStart(2, "0"); // 获取分钟，并补零
  const seconds = String(now.getSeconds()).padStart(2, "0"); // 获取秒数，并补零

  // 拼接成指定的格式
  return `${year}${month}${day}${hours}${minutes}${seconds}`;
};

export default {
  stringFromDate,
  stringFromDateTime,
  getCurrentTimestamp,
  getCurrentTimestamp2,
};
